from fastapi import FastAPI, Query
from path import Path
from pydantic import BaseModel, field_validator
import uvicorn

"""
    2. 对学生管理系统提供API接口
        a. 查询所有学生的信息
        b. 根据学生编号查询某个学生的信息
        c. 添加学生信息
        d. 根据学生姓名查询学生的信息
    3. 根据学生编号更新学生的信息
"""

app = FastAPI()
# 学生数据模型
class Student(BaseModel):
    学生编号: int
    学生班级: int
    姓名: str
    籍贯: str
    毕业院校: str
    专业: str
    入学时间: str
    毕业时间: str
    学历: str
    顾问编号: int
    年龄: int
    性别: str

# 模拟数据库
students_db = []
baseInfo_T = [
    {
        "学生编号": 23022801,
        "学生班级": 230228,
        "姓名": "鲁大师",
        "籍贯": "湖南",
        "毕业院校": "湖南工学院",
        "专业": "应用数学",
        "入学时间": "2015-09-01",
        "毕业时间": "2019-07-01",
        "学历": "本科",
        "顾问编号": 20003,
        "年龄": 27,
        "性别": "男"
    },
    {
        "学生编号": 23100701,
        "学生班级": 231007,
        "姓名": "紫薇格格",
        "籍贯": "湖南",
        "毕业院校": "湖南大学",
        "专业": "材料物理",
        "入学时间": "2016-09-01",
        "毕业时间": "2020-07-01",
        "学历": "本科",
        "顾问编号": 20004,
        "年龄": 26,
        "性别": "女"
    },
    {
        "学生编号": 23100702,
        "学生班级": 231007,
        "姓名": "小懂师兄",
        "籍贯": "广东",
        "毕业院校": "广州大学",
        "专业": "土木工程",
        "入学时间": "2016-09-01",
        "毕业时间": "2020-07-01",
        "学历": "本科",
        "顾问编号": 20005,
        "年龄": 26,
        "性别": "男"
    },
    {
        "学生编号": 23051802,
        "学生班级": 230518,
        "姓名": "张主席",
        "籍贯": "广东",
        "毕业院校": "深圳大学",
        "专业": "金融学",
        "入学时间": "2016-09-01",
        "毕业时间": "2020-07-01",
        "学历": "本科",
        "顾问编号": 20005,
        "年龄": 27,
        "性别": "男"
    },
    {
        "学生编号": 23051804,
        "学生班级": 230518,
        "姓名": "晓静姐姐",
        "籍贯": "河南",
        "毕业院校": "深圳大学",
        "专业": "会计学",
        "入学时间": "2018-09-01",
        "毕业时间": "2022-07-01",
        "学历": "本科",
        "顾问编号": 20005,
        "年龄": 23,
        "性别": "女"
    },
    {
        "学生编号": 23072401,
        "学生班级": 230724,
        "姓名": "王思记",
        "籍贯": "辽宁",
        "毕业院校": "广州大学",
        "专业": "材料物理",
        "入学时间": "2012-09-01",
        "毕业时间": "2016-07-01",
        "学历": "本科",
        "顾问编号": 20005,
        "年龄": 31,
        "性别": "男"
    }
]
job_info = [
    {
        "学生编号": "23051804",
        "就业开放时间": "2024-01-11",
        "OFFER下发时间": "2024-03-12",
        "就业公司名称": "深圳市中软有限公司",
        "就业薪资": 13000
    },
    {
        "学生编号": "23051802",
        "就业开放时间": "2024-01-15",
        "OFFER下发时间": "2024-03-12",
        "就业公司名称": "深圳市xx有限公司",
        "就业薪资": 15000
    }
]
# 初始化数据
for data in baseInfo_T:
    students_db.append(Student(**data))


# a. 查询所有学生的信息
@app.get("/students/list")
def get_all_students():
    return students_db


# b. 根据学生编号查询某个学生的信息
@app.get("/students/id/{student_id}")
def get_student_by_id(student_id: int):
    for student in students_db:
        if student.学生编号 == student_id:
            return student
    return {"message": "学生不存在", "student": None}


# c. 添加学生信息
@app.post("/students")
def add_student(student: Student):
    # 检查学生编号是否重复
    for existing_student in students_db:
        if existing_student.学生编号 == student.学生编号:
            return {"message": "学生编号已存在", "student": None}

    students_db.append(student)
    return {"message": "学生添加成功", "student": student}

# d. 根据学生姓名查询学生的信息
@app.get("/students/name/{student_name}")
def get_student_by_name(student_name: str):
    result = [student for student in students_db if student_name in student.姓名]
    if not result:
        return {"message": "未找到该姓名的学生", "students": None}
    return {"students": result}

# e. 根据学生编号更新学生的信息
@app.put("/students/id/{student_id}")
def update_student(student_id: int, updated_student: Student):
    for index, student in enumerate(students_db):
        if student.学生编号 == student_id:
            students_db[index] = updated_student
            return {"message": "学生信息更新成功", "student": updated_student}
    return {"message": "学生不存在", "student": None}

if __name__ == '__main__':
    uvicorn.run('0421作业:app', host='localhost', port=8000, reload=True)